package views
{
	import ctimer.*;
	
	import flash.display.InteractiveObject;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.sampler.NewObjectSample;
	import flash.system.System;
	import flash.text.ReturnKeyLabel;
	import flash.utils.Timer;
	
	import flashx.textLayout.debug.assert;
	
	import logic.PPTControlManager;
	
	import mx.core.IFactory;
	
	import org.gestouch.core.IGestureDelegate;
	import org.gestouch.core.Touch;
	import org.gestouch.events.GestureEvent;
	import org.gestouch.gestures.Gesture;
	import org.gestouch.gestures.PanGesture;
	import org.gestouch.gestures.PanGestureDirection;
	import org.gestouch.gestures.SwipeGesture;
	import org.gestouch.gestures.SwipeGestureDirection;
	
	import spark.components.TextArea;

	public class GestureController
	{
		
		
		private var _longPressGesture:org.gestouch.gestures.LongPressGesture;
		private var _tapGesture:org.gestouch.gestures.TapGesture;
		var _swipeGesture:org.gestouch.gestures.SwipeGesture;
		var _panGesture:org.gestouch.gestures.PanGesture;
		var _logArea:TextArea;

	
		var _pptControlManager:PPTControlManager;
		var _controlToListen:InteractiveObject;
		public function GestureController(controlToListen:InteractiveObject,logArea:TextArea,pptControlManager:PPTControlManager)
		{
			_longPressGesture = new org.gestouch.gestures.LongPressGesture(controlToListen);
			_longPressGesture.addEventListener(org.gestouch.events.GestureEvent.GESTURE_BEGAN, onLongPress);
			
			_tapGesture = new org.gestouch.gestures.TapGesture(controlToListen);
			_tapGesture.numTapsRequired=1;
			_tapGesture.numTouchesRequired=1;
			_tapGesture.addEventListener(org.gestouch.events.GestureEvent.GESTURE_RECOGNIZED,onTap);
			
			_swipeGesture = new org.gestouch.gestures.SwipeGesture(controlToListen);
			_swipeGesture.minOffset = 150;
			_swipeGesture.minVelocity =2;
			_swipeGesture.addEventListener(org.gestouch.events.GestureEvent.GESTURE_RECOGNIZED,onSwipe);
			_swipeGesture.direction = org.gestouch.gestures.SwipeGestureDirection.HORIZONTAL;	

//			_swipeGesture.delegate = this;
			
			
	/*		_panGesture = new org.gestouch.gestures.PanGesture(controlToListen);
			_panGesture.direction = org.gestouch.gestures.PanGestureDirection.HORIZONTAL|org.gestouch.gestures.PanGestureDirection.VERTICAL;
			_panGesture.minNumTouchesRequired =1;
			_panGesture.slop =20;
			_panGesture.addEventListener(org.gestouch.events.GestureEvent.GESTURE_RECOGNIZED,onPan);
			_panGesture.delegate=this;*/

			controlToListen.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			controlToListen.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			_logArea = logArea;
			_pptControlManager = pptControlManager;
			_controlToListen = controlToListen;
			
			
		}
		
		
		private function onMouseDown(event:MouseEvent):void
		{
			var logicPoint:Point = 	screenToLogic(new Point(event.localX,event.localY));
			log(logicPoint.x+":"+logicPoint.y+"local"+event.localX+":"+event.localY);
			_pptControlManager.locateMouse(logicPoint.x,logicPoint.y);
		}
		var _lastMove:Date =new Date();
		const MIN_MOUSEMOVE_SAMPLING:Number=300;
		private function onMouseMove(event:MouseEvent):void
		{
			if(event.buttonDown)
			{
				var now:Date = new Date();
				if(now.time-_lastMove.time>=MIN_MOUSEMOVE_SAMPLING)
				{
					_lastMove = now;
					var logicPoint:Point = 	screenToLogic(new Point(event.localX,event.localY));
					log(logicPoint.x+":"+logicPoint.y+"local"+event.localX+":"+event.localY);
					_pptControlManager.locateMouse(logicPoint.x,logicPoint.y);	
				}
				
			}
		}
		
		private function screenToLogic(point:Point):Point
		{
			var result:Point = new Point();
			
			result.x = point.x/_controlToListen.width;
			result.y = point.y/_controlToListen.height;
			
			result.x = Math.min(result.x,1);
			result.y = Math.min(result.y,1);
			
			result.x = Math.max(result.x,0);
			result.y = Math.max(result.y,0);
		 	return result;
		}
		
		
		private function onPan(event:org.gestouch.events.GestureEvent):void
		{
			var gesture:PanGesture = event.target as PanGesture;
			log(gesture);
			

			var logicPoint:Point = screenToLogic(gesture.location);
			log(logicPoint.x+":"+logicPoint.y);
			_pptControlManager.locateMouse(logicPoint.x,logicPoint.y);
			
		}
		
		
		
		
		private function onSwipe(event:org.gestouch.events.GestureEvent):void
		{
			//log(event.target);
			var  gesture:org.gestouch.gestures.SwipeGesture=(org.gestouch.gestures.SwipeGesture)(event.target);
			//log(gesture.direction+":"+gesture.offsetX+":"+gesture.offsetY);
			
			
			if(gesture.offsetX>0)
			{
				_pptControlManager.controlPPT(PPTControlManager.PPTCONTROL_RIGHT);	
			}
			else
			{
				_pptControlManager.controlPPT(PPTControlManager.PPTCONTROL_LEFT);	
			}
			
			
			
			
		}
		private function onLongPress(event:org.gestouch.events.GestureEvent):void
		{
			//log(event.target);
			// TODO Auto-generated method stub
	
			_pptControlManager.ZoomIn();

			log("zoom in");
				
		
		}
		
		private function onTap(event:org.gestouch.events.GestureEvent):void
		{
	
			_pptControlManager.ZoomOut();

			log("zoom out");
				
		
		}
		
		private function log(content:Object):void
		{
			return;
			if(content==null)
				return;
			
			if(_logArea.text==null)
			{
				_logArea.text = "";
			}
			_logArea.appendText(content.toString()+"\r\n");
			_logArea.invalidateDisplayList();
			
		}

	}
}